package net.e_agri.json;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.arnx.jsonic.JSON;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FriendsCommunitiesConverter extends AbstractContentConverter implements ContentConverter {

	private static final Log log = LogFactory.getLog(FriendsCommunitiesConverter.class);

	@Override
	public String parseSource(Source source, String url) {

		@SuppressWarnings("rawtypes")
		Map<String, List> contentMap = new LinkedHashMap<String, List>();
		List<String> fieldsList = Arrays.asList("name","url","images");
		contentMap.put("fields", fieldsList);
		List<List<Object>> contentList = new ArrayList<List<Object>>();
		contentMap.put("data", contentList);

		List<Object> record = new ArrayList<Object>();
		List<String> imgs = new ArrayList<String>();
		Map<String, String> recordMap = null;
		Map<String, Map<String, String>> recordListMap = new LinkedHashMap<String, Map<String, String>>();

		String cacheUrl = null;
		String imageUrl = null;
		
		String lastUrl = null;
		List<Element> trList = source.getAllElements(HTMLElementName.TR);
		if(url.indexOf("a=page_fh_album_list") != -1)
			trList = source.getAllElements(HTMLElementName.TD);

		for(Element tr : trList){
			if("photo".equals(tr.getAttributeValue("class"))){
				
				for(Element a : tr.getAllElements(HTMLElementName.A)){
					
					if(!recordListMap.containsKey(a.getAttributeValue("href"))){
						
						recordMap = new LinkedHashMap<String, String>();
						recordMap.put("url", a.getAttributeValue("href").substring(a.getAttributeValue("href").indexOf("&a=")+1));
						lastUrl = a.getAttributeValue("href");
	
						imageUrl = uriRoot + a.getFirstElement(HTMLElementName.IMG).getAttributeValue("src").replace("&m=pc", "");
						cacheUrl = getImageCacheUrl(imageUrl);
						if(cacheUrl != null)
							recordMap.put("images", cacheUrl);
						else
							recordMap.put("images", imageUrl);

						log.debug(recordMap.toString());
						recordListMap.put(a.getAttributeValue("href"), recordMap);
					}
				}
			}else if("text".equals(tr.getAttributeValue("class"))){

				log.debug("tr:text lastUrl = " + lastUrl);

				for(Element a : tr.getAllElements(HTMLElementName.A)){
					if(recordListMap.containsKey(a.getAttributeValue("href"))){
						recordMap = recordListMap.get(a.getAttributeValue("href"));
						recordMap.put("name", a.getTextExtractor().toString());
					}
				}

			}else if(url.indexOf("a=page_fh_album_list") != -1
				&& "2".equals(tr.getAttributeValue("colspan"))){

				log.debug("lastUrl = " + lastUrl);
				log.debug(tr.getTextExtractor().toString());

				recordMap = recordListMap.get(lastUrl);
				if(!recordMap.containsKey("name"))
					recordMap.put("name", tr.getTextExtractor().toString());
			}
		}

		for(String href : recordListMap.keySet()){
			recordMap = recordListMap.get(href);			
			record = new ArrayList<Object>();
			imgs = new ArrayList<String>();

			for(String field : fieldsList){
				if(field.equals("images")){
					imgs.add(recordMap.get(field));
					record.add(imgs);
				}else{
					record.add(recordMap.get(field));
				}
			}
			contentList.add(record);
		}
		log.debug("\n\t" + contentMap.toString());

		return JSON.encode(contentMap);
	}

}
